//
//  JMOrderVC.m
//  NewiOSProject
//
//  Created by MAC on 2023/9/8.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMOrderVC.h"
#import "JMOrderCell.h"
#import "JMOrderModel.h"
#import "JMOrderInfoVC.h"
#import "JMSlicenseVC.h"
#import "JMChangeMyBankController.h"


@interface JMOrderVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *contV;
    NSInteger type;
}
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataList;
@property (nonatomic, strong)UIView *emptyView;

@end

@implementation JMOrderVC
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.rdv_tabBarController setTabBarHidden:YES animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.rdv_tabBarController setTabBarHidden:NO animated:YES];
    [self getDataList];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    type = 1;
    self.TitleLabel.text = @"Pedidos";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.emptyView];
//    [self.view addSubview:self.topView];
    [self getDataList];
    [self.backBtn setImage:nil forState:(UIControlStateNormal)];
}

-(void)getDataList{
    [self startAnimating: nil];
    NSDictionary *param = @{@"pageNum":@(self.index).stringValue,@"pageSize":@"10",@"type":@(type).stringValue};
    
    [[JMBaseRequest sharedInstance] requestBasePost:BasePostRequestUrl(@"/api/order/list2") params:param success:^(NSDictionary * _Nullable dataDic, NSArray * _Nullable dataArray) {
        [self stopAnimating];
        NSMutableArray *list = [JMOrderModel mj_objectArrayWithKeyValuesArray:dataDic[@"orderRecordList"]];;
        [self.tableView.mj_header endRefreshing];
        if(self.index==1){
            self.dataList = @[].mutableCopy;
            self.dataList = [JMOrderModel mj_objectArrayWithKeyValuesArray:dataDic[@"orderRecordList"]];
        }else{
            [self.dataList addObjectsFromArray:list];
        }
        if (self.dataList.count) {
            self.emptyView.hidden = YES;
            _tableView.tableFooterView = self.footerView;
        }else{
            [JMCommonMethod uploadStatistics:@"EmptyRepayment" actionType:@"Enter" parameters:nil];

            self.emptyView.hidden = NO;
            _tableView.tableFooterView = nil;
        }
        if(list.count<10){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView reloadData];
    } fail:^(NSString * _Nonnull errorMsg) {
        [self stopAnimating];
    }];
    
}
//MARK: -UITableViewDataSource UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JMOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JMOrderCell" forIndexPath:indexPath];
    cell.selectionStyle = 0;
    if (indexPath.row < self.dataList.count) {
        JMOrderModel *model = self.dataList[indexPath.row];
           cell.model = model;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    JMOrderModel *model = self.dataList[indexPath.row];
    return model.rowHeight;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    JMOrderModel *model = self.dataList[indexPath.row];
    if(model.status.intValue==169 ||model.status.intValue==110){
       

    }else{
        JMOrderInfoVC *vc = JMOrderInfoVC.new;
        vc.listModel = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
  
}

//MARK: -lazyLoad
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kAppW(), kAppH()-NAVIGATION_BAR_HEIGHT()-TAB_BAR_HEIGHT()-55) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[JMOrderCell class] forCellReuseIdentifier:@"JMOrderCell"]; // 注册UITableViewCell
        _tableView.separatorColor = UIColor.clearColor;
        _tableView.sectionHeaderTopPadding = 0;
        _tableView.backgroundColor = Hex_Str_COLOR(@"#F6F9FE");
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [self loadMore:NO];
        }];
        header.lastUpdatedTimeLabel.hidden = YES;
        header.stateLabel.hidden = YES;
        _tableView.mj_header = header;
        MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
            [self loadMore:YES];
        }];
        [footer setTitle:@"Pull down to refresh" forState:MJRefreshStateIdle];
        [footer setTitle:@"Release to refresh" forState:MJRefreshStatePulling];
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"" forState:MJRefreshStateNoMoreData];
      
        _tableView.footer = footer;
    }
    return _tableView;
}

-(void)loadMore:(BOOL)isMore{
    if(isMore){
        self.index ++;
    }else{
        self.index = 1;
    }
    [self getDataList];
}

-(UIView *)emptyView{
    if (!_emptyView) {
        _emptyView = [[UIView alloc]initWithFrame:(CGRectMake(0, _tableView.top, kAppH(),self.tableView.height))];
        _emptyView.backgroundColor = Hex_Str_COLOR(@"#F7F7F5");
        UIImageView *imgV = [[UIImageView alloc]initWithFrame:(CGRectMake(0, 0, 140, 140))];
        [imgV setImage:ImageNamed(@"空的")];
        [_emptyView addSubview:imgV];;
        imgV.center = CGPointMake(kAppW()/2, _emptyView.height/2);
        UILabel *lab = [JMControl createLabelWithFrame:(CGRectMake(0, imgV.bottom+25, kAppW(), FontH(18))) Font:Font(15) Text:@"Está vacío ahora, vuelva más tarde" color:Hex_Str_COLOR(@"#3C57F8")];
        [_emptyView addSubview:lab];
        lab.textAlignment  = 1;
        
        UIButton *delegateBtn = [JMControl createButtonWithFrame:(CGRectMake(0, _emptyView.height-67, kAppW(), 37)) Target:self Action:@selector(delegateBtnClick) Title:@"《 Politica de privacidad 》" titlColor:Hex_Str_COLOR(@"#222222") font:Font(12) tag:1 backgroundColor:UIColor.clearColor];
        [_emptyView addSubview:delegateBtn];
    }
    return _emptyView;
}
@end
